Page({

  /**
   * 页面的初始数据
   */
  data: {
      rUnit:['pΩ','nΩ','μΩ','mΩ','Ω','kΩ','MΩ','GΩ'],
      rValue:100,
      rUnitIndex:4,
      vUnit:['pV','nV','μV','mV','V','kV','MV','GV'],
      vValue:100,
      vUnitIndex:4,
      iUnit:['pA','nA','μA','mA','A','kA','MA','GA'],
      iValue:100,
      iUnitIndex:4,
      p:100,
      // index:4,
      rValueBingOne:100,
      rValueBingTwo:100,
      rValueBingThree:100,
      rValueBingFour:100,
      rValueBingAll:100,
      indexBing:4,
      jinsi:["E6","E12","E24","E48","E96","E192"],
      jinsiIndex:0,
      jinsiValue:10,
      E6:[100,150,220,330,470,680],
      E12:[100,120,150,180,220,270,330,390,470,560,680,820],
      E24:[100,110,120,130,150,160,180,200,220,240,270,300,330,360,390,430,470,510,560,620,680,750,820,910]
      E48:[100,102,105,107,110,113,115,118,121,124,127,130,133,137,140,143,147,150,154,158,162,165,169,178,182,187,191,196,200,205,210,215,221,226,232,237,243,240,255,261,267,274,287,301,316,332,348,365,383,402,430,470,510,560,620,680,750,820,910]

  },
  onLoad:function(){
  
  
  },
  //选择单位
    rUnit:function(e){
        this.setData({
            rUnitIndex:e.detail.value
        })
    },
    vUnit:function(e){
        this.setData({
            vUnitIndex:e.detail.value
        })
    },
    iUnit:function(e){
        this.setData({
            iUnitIndex:e.detail.value
        })
    },
  //双向数据绑定
  r:function(e){
    if(String(e.detail.value).indexOf(".")==-1){
      if(String(e.detail.value).length>3){
        wx.showModal({
           title: '提示',
                content: '实数部分不能超过3位',
        })
       this.setData({
        rValue:100
      })
        return
      }
    }
    
    this.setData({
      rValue:e.detail.value
    })
    if(e.detail.value>1&&e.detail.value<10){
      if(this.data.jinsiIndex==0){
        var e6=[];
        for(let i=0;i<this.data.E6.length;i++){
          e6.push((this.data.E6[i]*0.01).toFixed(2));
        }
        console.log(e6)
       for(let i=0;i<e6.length;i++){
          var tmp1=e.detail.value-parseFloat(e6[i]);
          var j=i;
          var tmp2=e.detail.value-parseFloat(e6[++j]);
          console.log(tmp1)
          console.log(tmp2)
          if(tmp1==0){
            this.setData({
                jinsiValue:parseFloat(e6[i])
              })
            break;
          }
          if(tmp2==0){
            this.setData({
                jinsiValue:parseFloat(e6[++i])
              })
            break;
          }
          if(tmp1>0&&tmp2<0){
            //判断绝对值大小
            if(-(tmp1)>(tmp2)){
              this.setData({
                jinsiValue:parseFloat(e6[i])
              })
            }else{
              this.setData({
                jinsiValue:parseFloat(e6[++i])
              })
            }
            break;
          }else{
            if(i==e6.length-1){
              console.log(parseFloat(e6[e6.length-1]))
              this.setData({
                jinsiValue:parseFloat(e6[e6.length-1])
              })
              console.log(this.data.jinsiValue)
            }
          }
        }
      }
    }
  },
  i:function(e){
    
    if(String(e.detail.value).indexOf(".")==-1){
      if(String(e.detail.value).length>3){
        wx.showModal({
          title: '提示',
          content: '实数部分不能超过3位',
        })
       this.setData({
        iValue:100
      })
        return
      }
    }
    this.setData({
      iValue:e.detail.value
    })
  },
  v:function(e){
    if(String(e.detail.value).indexOf(".")==-1){
      if(String(e.detail.value).length>3){
        wx.showModal({
          title: '提示',
                content: '实数部分不能超过3位',
        })
       this.setData({
        vValue:100
      })
        return
      }
    }
    this.setData({
      vValue:e.detail.value
    })
  },
//button按钮点击后计算
computeR:function(e){

  var iUnitIndex=this.data.iUnitIndex;
  var vUnitIndex=this.data.vUnitIndex;
  var number=vUnitIndex-iUnitIndex;
  console.log(number)
  if(number<-4||number>4){
    wx.showModal({
          title: '提示',
          content: '超出范围',
    })
    this.setData({
      iUnitIndex:4,
      vUnitIndex:4
    })
    return
  }
  if(iUnitIndex==0){
      wx.showModal({
        title: '提示',
        content: '电流不能为0',
      })
      this.setData({
        iValue:100
      })
      return;
  }
  console.log(vUnitIndex)
  if(number==0){
    this.setData({
      rUnitIndex:4
    });
    
  }
  if(number==1){
    this.setData({
      rUnitIndex:5
    });
  }
   if(number==2){
    this.setData({
      rUnitIndex:6
    });
  }
   if(number==3){
    this.setData({
      rUnitIndex:7
    });
  }
   if(number==-1){
    this.setData({
      rUnitIndex:3
    });
  }
   if(number==-2){
    this.setData({
      rUnitIndex:2
    });
  }
   if(number==-3){
    this.setData({
      rUnitIndex:1
    });
  }
  //计算r=u/i
  var value=this.data.vValue/this.data.iValue;
  if(String(value).indexOf(".")==-1){
  }else{
    value=value.toFixed(3)
  }
  var rUnitIndex=this.data.rUnitIndex;
  if(value>999){
    this.setData({
      rUnitIndex:parseInt(rUnitIndex)+1
    })
    value=(value/1000).toFixed(3);
  }
  if(value<1){
    this.setData({
      iUnitIndex:parseInt(iUnitIndex)-1
    })
    value=value*1000;
  }
  this.setData({
    rValue:value
  })
  var p=this.data.rValue*this.data.iValue;
  this.setData({
    p:p
  })
},
computeV:function(e){
  var iUnitIndex=this.data.iUnitIndex;
  var rUnitIndex=this.data.rUnitIndex;
  var number=parseInt(rUnitIndex)+parseInt(iUnitIndex);
  console.log(number);
  if(number<3||number>12){
    wx.showModal({
          title: '提示',
          content: '超出范围',
        })
    this.setData({
      iUnitIndex:4,
      rUnitIndex:4
    })
    return
  }
  if(number==7){
    this.setData({
      vUnitIndex:3
    });
    
  }
  if(number==8){
    this.setData({
      vUnitIndex:4
    });
  }
   if(number==9){
    this.setData({
      vUnitIndex:5
    });
  }
   if(number==10){
    this.setData({
      vUnitIndex:6
    });
  }
   if(number==11){
    this.setData({
      vUnitIndex:7
    });
  }
   if(number==6){
    this.setData({
      vUnitIndex:2
    });
  }
   if(number==5){
    this.setData({
      vUnitIndex:1
    });
  }
  if(number==4){
    this.setData({
      vUnitIndex:0
    });
  }
  //计算r=u/i
  var value=this.data.rValue*this.data.iValue;
  if(String(value).indexOf(".")==-1){
  }else{
    value=value.toFixed(3)
  }
  var vUnitIndex=this.data.vUnitIndex;
  if(value>999){
    this.setData({
      vUnitIndex:parseInt(vUnitIndex)+1
    })
    value=(value/1000).toFixed(3);
  }
  if(value<1){
    this.setData({
      iUnitIndex:parseInt(iUnitIndex)-1
    })
    value=value*1000;
  }
  this.setData({
    vValue:value
  })
  var p=this.data.rValue*this.data.iValue;
  this.setData({
    p:p
  })
},
computeI:function(e){  
  if(this.data.rValue==0){
      wx.showModal({
       title: '提示',
                content: '电阻不能为0',
      })
      this.setData({
        rValue:100
      })
      return;
    }
  var rUnitIndex=this.data.rUnitIndex;
  var vUnitIndex=this.data.vUnitIndex;
  var number=vUnitIndex-rUnitIndex;
  console.log(number)
  if(number<-4||number>4){
    wx.showModal({
          title: '提示',
          content: '超出范围',
    })
    this.setData({
      rUnitIndex:4,
      vUnitIndex:4
    })
    return
  }
  if(number==0){
    this.setData({
      iUnitIndex:4
    });
    
  }
  if(number==1){
    this.setData({
      iUnitIndex:5
    });
  }
   if(number==2){
    this.setData({
      iUnitIndex:6
    });
  }
   if(number==3){
    this.setData({
      iUnitIndex:7
    });
  }
   if(number==-1){
    this.setData({
      iUnitIndex:3
    });
  }
   if(number==-2){
    this.setData({
      iUnitIndex:2
    });
  }
   if(number==-3){
    this.setData({
      iUnitIndex:1
    });
  }
  var value=this.data.vValue/this.data.rValue;
  if(String(value).indexOf(".")==-1){
  }else{
    value=value.toFixed(3)
  }
  var iUnitIndex=this.data.iUnitIndex;
  if(value>999){
    this.setData({
      iUnitIndex:parseInt(iUnitIndex)+1
    })
    value=(value/1000).toFixed(3);
  }
  if(value<1){
    this.setData({
      iUnitIndex:parseInt(iUnitIndex)-1
    })
    value=value*1000;
  }
  this.setData({
    iValue:value
  })
  var p=this.data.rValue*this.data.iValue;
  this.setData({
    p:p
  })
},
//并联
//双向数据绑定
rBingOne:function(e){
  if(e.detail.value==0){
      wx.showModal({
        title: '提示',
        content: '电阻不能为0',
      })
      this.setData({
        rValue:100
      })
      return;
    }
    if(String(e.detail.value).indexOf(".")==-1){
      if(String(e.detail.value).length>3){
        wx.showModal({
          title: '提示',
          content: '实数部分不能超过3位',
        })
        this.setData({
        rValue:100
      })
        return
      }
    }
    this.setData({
      rValueBingOne:e.detail.value
    })
},
rBingTwo:function(e){
  if(e.detail.value==0){
      wx.showModal({
        title: '提示',
        content: '电阻不能为0',
      })
     this.setData({
        rValue:100
      })
      return;
    }
    if(String(e.detail.value).indexOf(".")==-1){
      if(String(e.detail.value).length>3){
        wx.showModal({
          title: '提示',
          content: '实数部分不能超过3位',
        })
       this.setData({
        rValue:100
      })
        return
      }
    }
    this.setData({
      rValueBingTwo:e.detail.value
    })
},
rBingThree:function(e){
  if(e.detail.value==0){
      wx.showModal({
        title: '提示',
        content: '电阻不能为0',
      })
      this.setData({
        rValue:100
      })
      return;
    }
    if(String(e.detail.value).indexOf(".")==-1){
      if(String(e.detail.value).length>3){
        wx.showModal({
          title: '提示',
          content: '实数部分不能超过3位',
        })
       this.setData({
        rValue:100
      })
        return
      }
    }
    this.setData({
      rValueBingThree:e.detail.value
    })
},
rBingFour:function(e){
  if(e.detail.value==0){
      wx.showModal({
        title: '提示',
        content: '电阻不能为0',
      })
      this.setData({
        rValue:100
      })
      return;
    }
    if(String(e.detail.value).indexOf(".")==-1){
      if(String(e.detail.value).length>3){
        wx.showModal({
          title: '提示',
          content: '实数部分不能超过3位',
        })
       this.setData({
        rValue:100
      })
        return
      }
    }
    this.setData({
      rValueBingFour:e.detail.value
    })
},
//改变单位
UnitBing:function(e){
  this.setData({
    indexBing:e.detail.value
  })
},
//计算结果
computeBing:function(e){
  if(this.data.rValueBingOne!=""){
    var R1=(1/this.data.rValueBingOne);
  }
  if(this.data.rValueBingTwo!=""){
    var R2=(1/this.data.rValueBingTwo);
  }
  if(this.data.rValueBingThree!=""){
    var R3=(1/this.data.rValueBingThree);
  }
  if(this.data.rValueBingFour!=""){
    var R4=(1/this.data.rValueBingFour);
  }
  var total = R1+R2+R3+R4;
      total = 1/total;
    if(String(total).indexOf(".")==-1){
    
    }else{
      total=total.toFixed(3)
    }
    this.setData({
      rValueBingAll:total
    })
},
//近似值
jinsi:function(e){
  this.setData({
    jinsiIndex:e.detail.value
  })
}
})